using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text.RegularExpressions;

namespace QuickTest.Core.Compilation
{
    /// <summary>
    /// Represents a Visual Studio solution for the purposes of QuickTest
    /// </summary>
    public class Solution
    {
        #region Static

        private const string ValidSolutionRegExPattern = "Microsoft Visual Studio Solution File, Format Version";
        private const string ProjectPathRegExPattern = "\".*?(?<=\".*?)(\\.csproj|\\.vbproj)";
        private const string SolutionFileNameRegExPattern = "(?<1>.*/)";

        #endregion

        #region Construction

        public static Solution Create(FileUri fileUri, ITextRetriever textRetriever)
        {
            return new Solution(fileUri, textRetriever);
        }

        private Solution(FileUri fileUri, ITextRetriever textRetriever)
        {
            if (fileUri == null)
                throw new ArgumentNullException("fileUri");

            if (textRetriever == null)
                throw new ArgumentNullException("textRetriever");

            FileUri = fileUri;
            TextRetriever = textRetriever;
        }

        #endregion

        #region Fields

        private ReadOnlyCollection<Project> projects;

        #endregion

        #region Properties

        private FileUri FileUri { get; set; }

        private ITextRetriever TextRetriever { get; set; }

        /// <summary>
        /// Returns the projects associated with this solution
        /// </summary>
        public ReadOnlyCollection<Project> Projects
        {
            get
            {
                if (projects == null)
                {
                    projects = GetProjects();
                }

                return projects;
            }
        }

        #endregion

        #region Methods

        private ReadOnlyCollection<Project> GetProjects()
        {
            string solutionFileText = GetSolutionFileText();

            var projectList = new List<Project>();

            //gets all relative project paths from the solution file
            var regEx = new Regex(ProjectPathRegExPattern, RegexOptions.RightToLeft);
            foreach (Match match in regEx.Matches(solutionFileText))
            {
                //regex leaves a preceding quotation mark
                Project project = Project.Create(GetProjectFileUri(match.Value.Substring(1)));
                projectList.Add(project);
            }

            return new ReadOnlyCollection<Project>(projectList);
        }

        private string GetSolutionFileText()
        {
            string solutionFileText = TextRetriever.GetText();

            var validSolutionRegEx = new Regex(ValidSolutionRegExPattern, RegexOptions.RightToLeft);

            if (! validSolutionRegEx.IsMatch(solutionFileText))
                throw new CompilationException("Invalid solution file provided");

            return solutionFileText;
        }

        private FileUri GetProjectFileUri(string projectPath)
        {
            var regEx = new Regex(SolutionFileNameRegExPattern);
            return new FileUri(regEx.Match(FileUri.AbsolutePath).Value + projectPath);
        }

        #endregion
    }
}